#pragma once

#include <utility/md5.h>
#include <commondef/glb_msg_define.h>
#include <interface/file_trans_client_interface.h>

class md5_status_callback_impl : public IStatusCallback
{
public:
	md5_status_callback_impl(_AFT::isession * ptr_session, aot::tt::ievent_base* ptr_notify)
	{
		this->is_cancel_ = FALSE;
		this->ptr_session_ = ptr_session;
		this->ptr_notify_ = ptr_notify;
	}

	~md5_status_callback_impl(void){;}

	void set_msg(LPCTSTR ptr_msg){ this->ptr_session_->set_return_msg(ptr_msg); }

	virtual int OnProgress(ULONGLONG ulProgress, ULONGLONG ulProgressMax)
	{

		DWORD cur_time = 0;

		this->ptr_session_->set_file_size(ulProgressMax);

		if (last_trans_len_ != ulProgress)
		{
			if ( i++ % 8 == 0 )
			{
				cur_time = GetTickCount();
				if ( 0 == last_notify_time_ )
				{
					this->last_notify_time_ = cur_time;
				}

				if ( (cur_time - last_notify_time_) > 300  )
				{
					this->ptr_session_->set_transfered_size(ulProgress);

					if ( this->ptr_notify_ )
					{
						this->ptr_notify_->on_notify(WM_GLB_FTC_TO_UI_CHECK_FILE_MSG, (WPARAM)this->ptr_session_, NULL);
					}

					this->last_notify_time_ = cur_time;
				}	
			}
		}

		this->last_trans_len_ = ulProgress;

		if ( this->is_cancel_ )
		{
			return -1;
		}

		return 0;
	}

	void set_cancel(BOOL is_cancel = TRUE)
	{
		this->is_cancel_ = is_cancel;
	}

protected:
	BOOL is_cancel_; 
	DWORD last_notify_time_;
	_AFT::isession * ptr_session_; 
	aot::tt::ievent_base* ptr_notify_;
	int i;
	int last_trans_len_;
};
